from pylons import request
from decorator import decorator
from pylons.templating import render
from openlbsserver.lib import helpers as h
from xml.dom.minidom import parseString

def require_valid_xml_post(fn, *args, **kwargs):
    '''Make sure a xml doc is passed'''
    try:
        if len(request.POST['xml']) > 0:
            
            # check valid xml document
            doc = parseString(request.POST['xml'])
            
            # verify document element
            if doc.documentElement.tagName != 'openlbs':
                return h.abort_xml_error()
                
            # return function
            return fn(*args, **kwargs)
        else:
            return h.abort_xml_error()
    except Exception, msg:
        print msg
        return h.abort_xml_error()
    
require_valid_xml_post = decorator(require_valid_xml_post)
